package com.recommend.recommendkeyword.service.impl;

import com.recommend.recommendkeyword.common.CommonResponse;
import com.recommend.recommendkeyword.entity.User;
import com.recommend.recommendkeyword.mapper.UserMapper;
import com.recommend.recommendkeyword.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 注册用户
     * @param username
     * @param password
     * @param email
     * @param role
     * @return boolean
     */
    @Override
    public boolean register(String username, String password, String email, int role) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setRole(role);
        user.setVip(0);
        user.setSearchNum(0);

        Integer status = userMapper.insertUser(user);
        if (status == 1)
            return true;
        else
            return false;


    }

    /**
     * 判断用户名是否存在
     * @param username
     * @return boolean
     */
    @Override
    public boolean isExistUsername(String username) {
        if (userMapper.selectUserByUsername(username) == null)
            return false;
        return true;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return String
     */
    @Override
    public boolean login(String username, String password) {
        if (userMapper.selectUserByUsernameAndPassword(username, password) == null)
            return false;
        return true;
    }

    /**
     * 查看个人信息
     * @param userId
     * @return User
     */
    @Override
    public User getUser(int userId) {
        return userMapper.selectUserById(userId);
    }

    /**
     * 修改密码
     * @param userId
     * @param oldPassword
     * @param newPassword
     * @return boolean
     */
    @Override
    public String changePassword(int userId, String oldPassword, String newPassword) {
        if (userMapper.selectUserByIdAndPassword(userId,oldPassword) == null)
            return "原密码错误或不存在此账号!";
        Integer status = userMapper.changePasswordByUserId(userId,newPassword);
        if (status == 1)
            return "修改成功!";
        return "系统错误!";
    }

    /**
     * 修改VIP权限
     * @param userId
     * @return boolean
     */
    @Override
    public boolean changeVip(int userId) {
        Integer status = userMapper.changeVipByuUserId(userId,1);
        if (status == 1)
            return true;
        return false;
    }

    /**
     * 修改用户信息
     * @param userId
     * @param username
     * @param email
     * @return boolean
     */
    @Override
    public boolean changeUser(int userId, String username, String email) {
        Integer status = userMapper.changeUser(userId, username, email);
        if (status == 1)
            return true;
        return false;
    }
}
